'''
Created on 26 nov. 2010

@author: christianp
'''
import threading
from copy import copy
from LayerHandler import LayerHandler
from PropertyEvents import PropertyEvents

class PropertyHandler(object):
    pWindow = None
    
    DeselectLayer = None
    UpdateMainWindow = None
    UpdateMainWindowList = None
    SetSeedExtent = None
    SetLayerExtent = None
    
    layerHandler = None
    pEvents = None
    
    __lock = None
    
    lLayerMinX = None
    lLayerMinY = None
    lLayerMaxX = None
    lLayerMaxY = None
    
    lSeedMinX = None
    lSeedMinY = None
    lSeedMaxX = None
    lSeedMaxY = None
    
    lboxTileSeeds = None
    lboxZoomLevelsLayer = None
    lboxZoomLevelsSeed = None    
    
    gaugeTiles = None
    labelTilesToBeDone = None
    stxtGaugeDone = None
    labelTilesFailed = None
    stxtGaugeRemaining = None
    
    def __init__(self, pWindow):
        '''Constructor'''
        self.pWindow = pWindow
        self.layerHandler = LayerHandler()
        self.pEvents = PropertyEvents(self)
        self.__lock = threading.Lock()

    def AddTileLayers(self, layers):
        self.layerHandler.SetTileSeeds(layers)
        self.lboxTileSeeds.Clear()
        self.lboxZoomLevelsSeed.Clear()
        self.lboxZoomLevelsLayer.Clear()

        for i in range(0,len(layers)):
            seed = layers[i]
            self.lboxTileSeeds.Insert(seed.tile_schema.name,i,False)

        if len(self.lboxTileSeeds.Items) > 0:
            self.lboxTileSeeds.Select(0)
            self.pEvents.TileSeedSelected(self.lboxTileSeeds.Items[0])

    def GetSeedIndexInList(self, name):
        for i in range(0, len(self.lboxTileSeeds.Items)):
            item = self.lboxTileSeeds.Items[i]
            if item == name:
                return i
        return None
    
    def UpdateSeedExtent(self, extent):
        layer = self.layerHandler.GetTileSeed(self.lboxTileSeeds.Items[self.lboxTileSeeds.GetSelection()])
        if not(layer.active):
            layer.SetSeedExtent(extent)
    
            self.lSeedMinX.SetLabel(str(extent.getMinX()))
            self.lSeedMinY.SetLabel(str(extent.getMinY()))
            self.lSeedMaxX.SetLabel(str(extent.getMaxX()))
            self.lSeedMaxY.SetLabel(str(extent.getMaxY()))
            
            self.SetSeedExtent(extent)
            return True
        else:
            return False
    
    def UpdateSeeExtent(self, extent):
        layer = self.layerHandler.GetTileSeed(self.lboxTileSeeds.Items[self.lboxTileSeeds.GetSelection()])
        layer.SetSeedExtent(extent)

        self.lSeedMinX.SetLabel(str(extent.getMinX()))
        self.lSeedMinY.SetLabel(str(extent.getMinY()))
        self.lSeedMaxX.SetLabel(str(extent.getMaxX()))
        self.lSeedMaxY.SetLabel(str(extent.getMaxY()))
        
        self.SetSeedExtent(extent)
        
    def SetSeedInfo(self, seed):
        seed.SetSeedExtent(seed.tile_schema.extent)
        seed.SetSeedZoomLvl(seed.tile_schema.resolutions)
        #seedZoomLvl = seed.GetSeedZoomLvl()
        seedZoomLvl = seed.seed_schema.resolutions
        
        for i in range(0, len(seedZoomLvl)):
            item = seedZoomLvl[i]
            self.lboxZoomLevelsSeed.Insert(str(copy(item[0])), i, copy(item))
        #self.UpdateSeeExtent(seed.GetSeedExtent())
        self.UpdateSeeExtent(seed.seed_schema.extent)
        
    def SetLayerInfo(self, seed):
        for i in range(0, len(seed.tile_schema.resolutions)):
            self.lboxZoomLevelsLayer.Insert(str(i), i, seed.tile_schema.resolutions[i])

        self.lLayerMinX.SetLabel(str(seed.tile_schema.extent.getMinX()))
        self.lLayerMinY.SetLabel(str(seed.tile_schema.extent.getMinY()))
        self.lLayerMaxX.SetLabel(str(seed.tile_schema.extent.getMaxX()))
        self.lLayerMaxY.SetLabel(str(seed.tile_schema.extent.getMaxY()))
        
        self.SetLayerExtent(seed.tile_schema.extent)
        self.SetSeedInfo(seed)
     
    def AddSeedZoomLvl(self, zoomLvl):
        if (len(self.lboxZoomLevelsSeed.Items) > 0):
            for i in range(0, (len(self.lboxZoomLevelsSeed.Items))):
                value = self.lboxZoomLevelsSeed.Items[i]
                if  zoomLvl[0] < int(value):
                    self.lboxZoomLevelsSeed.Insert(str(zoomLvl[0]),i,zoomLvl)
                    return
            self.lboxZoomLevelsSeed.Insert(str(zoomLvl[0]),len(self.lboxZoomLevelsSeed.Items),zoomLvl)
        else:
            self.lboxZoomLevelsSeed.Insert(str(zoomLvl[0]),0,zoomLvl)
    
    def IsItemSelectedInList(self, list):
        for i in range(0, len(list.Items)):
            if (list.IsSelected(i)):
                return True
        return False
    
    def ClearLayerLabels(self):
        self.lboxZoomLevelsLayer.Clear()
        self.lLayerMinX.Clear()
        self.lLayerMinY.Clear()
        self.lLayerMaxX.Clear()
        self.lLayerMaxY.Clear()
    
    def ClearSeedLabels(self):
        self.lboxZoomLevelsSeed.Clear()
        self.lSeedMinX.Clear()
        self.lSeedMinY.Clear()
        self.lSeedMaxX.Clear()
        self.lSeedMaxY.Clear()
